package ruifight.member.service.security;

import java.util.ArrayList;
import java.util.Collection;

import javax.annotation.Resource;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import ruifight.member.model.MemberInfo;
import ruifight.member.model.MemberInfoExample;
import ruifight.member.model.ShopInfo;
import ruifight.member.model.ShopInfoExample;
import ruifight.member.model.UserInfo;
import ruifight.member.model.UserInfoExample;
import ruifight.member.model.security.MyUserDetail;
import ruifight.member.service.MemberInfoService;
import ruifight.member.service.ShopInfoService;
import ruifight.member.service.UserInfoService;

@Service
public class UserDetailsServiceImpl implements UserDetailsService{
	@Resource
	private UserInfoService userInfoService;
	@Resource
	private ShopInfoService shopInfoService;
	@Resource
	private MemberInfoService memberInfoService;
	@Override
	public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
		MyUserDetail userDetail=null;
		UserInfoExample userInfoExample=new UserInfoExample();
    	userInfoExample.createCriteria()
    		.andLoginNameEqualTo(userName)
    		.andUserStateEqualTo(1);
    	UserInfo userInfo=userInfoService.selectOneByExample(userInfoExample);
    	switch (userInfo.getUserType()) {
		case 2:
			userDetail=new MyUserDetail(userInfo.getLoginName(), userInfo.getPassword(), getAuthorities("ROLE_SHOP"));
			break;
		case 3:
			userDetail=new MyUserDetail(userInfo.getLoginName(), userInfo.getPassword(), getAuthorities("ROLE_USER"));
			break;
		default: 
			userDetail=new MyUserDetail(userInfo.getLoginName(), userInfo.getPassword(), getAuthorities("ROLE_MANAGER"));
		}
    	
    	userDetail.setUserName(userInfo.getUserName());
    	userDetail.setPwd(userInfo.getPassword());
    	userDetail.setMobile(userInfo.getMobile());
    	userDetail.setTelephone(userInfo.getTelephone());
    	userDetail.setUserType(userInfo.getUserType());
    	userDetail.setUserInfoId(userInfo.getUserInfoId());
    	userDetail.setCertificateCode(userInfo.getCertificateCode());
    	userDetail.setOpenId(userInfo.getOpenId());
    	if(userInfo.getUserType()==2) {
    		ShopInfoExample shopInfoExample=new ShopInfoExample();
        	shopInfoExample.createCriteria()
        		.andUserInfoIdEqualTo(userInfo.getUserInfoId())
        		.andShopStateEqualTo(1);
    		ShopInfo shopInfo=shopInfoService.selectOneByExample(shopInfoExample);
        	userDetail.setShopInfoId(shopInfo.getShopInfoId());
    	}else if(userInfo.getUserType()==3) {
    		MemberInfoExample memberInfoExample=new MemberInfoExample();
        	memberInfoExample.createCriteria()
    			.andUserInfoIdEqualTo(userInfo.getUserInfoId())
    			.andMemberStateEqualTo(1);
        	MemberInfo memberInfo=memberInfoService.selectOneByExample(memberInfoExample);
        	userDetail.setMemberInfoId(memberInfo.getMemberInfoId());
    	}
    	
		//查询用户是否存在
//		ManagerInfoExample managerInfoExample=new ManagerInfoExample();
//		Criteria criteria=managerInfoExample.createCriteria();
//		criteria.andLoginNameEqualTo(userName);
//		List<ManagerInfo> managerInfoList=managerInfoMapper.selectByExample(managerInfoExample);
//		if(managerInfoList.size()<=0){ 
//            throw new UsernameNotFoundException("用户名不存在");
//        }
//		ManagerInfo managerInfo=managerInfoList.get(0);
//		//查询用户所属机构（检查站）
//		OrganizationInfo organizationInfo=organizationInfoMapper.selectByPrimaryKey(managerInfo.getOrganizationInfoId());
//		if(managerInfo.getManagerType()==null) {
//			throw new UsernameNotFoundException("用户状态不可用");
//		}
//		//封装用户信息
//		MyUserDetail userDetail=null;
//		switch (managerInfo.getManagerType()) {
//		case MyConstant.MANAGER_TYPE_ROOT:
//			userDetail=new MyUserDetail(managerInfo.getLoginName(), managerInfo.getPassword(), getAuthorities("ROLE_ROOT"));
//			break;
//		case MyConstant.MANAGER_TYPE_ADMIN:
//			userDetail=new MyUserDetail(managerInfo.getLoginName(), managerInfo.getPassword(), getAuthorities("ROLE_ADMIN"));
//			break;
//		default: throw new UsernameNotFoundException("用户状态不可用");
//		}
//		userDetail.setManagerInfoId(managerInfo.getManagerInfoId());
//		userDetail.setManagerType(managerInfo.getManagerType());
//		userDetail.setOrganizationInfoId(organizationInfo.getOrganizationInfoId());
//		userDetail.setOrganizationName(organizationInfo.getOrganizationName());
//		userDetail.setOrganizationType(organizationInfo.getOrganizationType());
//		userDetail.setManagerName(managerInfo.getManagerName());
//		userDetail.setGender(managerInfo.getGender());
//		userDetail.setJobNumber(managerInfo.getJobNumber());
//		userDetail.setMobilePhone(managerInfo.getMobilePhone());
//		userDetail.setOfficePhone(managerInfo.getOfficePhone());
		return userDetail;
	}
	
	private Collection<GrantedAuthority> getAuthorities(String role) {
        Collection<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
        SimpleGrantedAuthority grantedAuthority = new SimpleGrantedAuthority(role);
        grantedAuthorities.add(grantedAuthority);
        return grantedAuthorities;
    }
	
}
